﻿var editor;


jQuery(document).ready(function ($) {
    // Code using $ as usual goes here.    

    $("#EditorPanel").dialog({ autoOpen: false, modal: true, width: 800, height: 600 });
    GetScript('', HandleGetScript);
    editor = jQuery("#EditorTextbox");
    //    
    //    // initialisation
    //    editAreaLoader.init({
    //        id: editor.attr("id")	// id of the textarea to transform		
    //    , start_highlight: true	// if start with highlight
    //    , allow_resize: "none"
    //    , allow_toggle: false
    //    , word_wrap: false
    //    , language: "en"
    //    , syntax: "js"
    //    , line_number: 0
    //    , toolbar: "new_document, |, search, go_to_line, |, undo, redo, |, select_font, |, change_smooth_selection, highlight, reset_highlight, |"
    //    });
});

function ScriptFixedUp() {
    var script = $("#EditorTextbox").val();
    script = encodeURI(encodeURIComponent(script));
    return script;
}
function FixedUpScript(script) {
    return decodeURI(decodeURIComponent(script));
}

function HandleGetScript(result) {
    $("#EditorTextbox").val(FixedUpScript(result));
}
function ApplyScript() {
    SaveScript(ScriptFixedUp(), ReceiveApply);
}

function CloseWindow() {
    $("#EditorPanel").dialog('close');
    var UpdatePanel = jQuery("#UpdatePanelParent:first-child");    
    __doPostBack(UpdatePanel.attr('ID'), '');
    return false;
}

function OpenEditor() {
    $("#EditorPanel").dialog('open');
    return false;
}
function SendScript() {
    ExecuteScript(ScriptFixedUp(), ReceiveScriptResults);
}
function ReceiveScriptResults(result) {
    $("#Results").html(result);
}
function ReceiveApply(result) {
    if (result == "True")
        $("#Results").html("Your script has been saved.");
    else
        $("#Results").html("Your script was not saved.<br /><br />" + result);
}

function FixEditorValue() {
    //alert(editAreaLoader.getValue(editID));
    editor.value = (editAreaLoader.getValue(editID));
}
